module.exports = function(e, t) {
    var a = require("../util"), r = null, n = a.getCurDomain(), i = function(e, t, r, i, o, s, p, c, l, g, d, u, f) {
        var h = a.J(o) || null, y = a.safetyCall(t, [ h, i ], null);
        if (!y) return !1;
        var E = y.code || s, v = !("success" in y) || y.success;
        e.api(r, v, p, E, y.msg, c, l, g, d, n, u, f);
    }, o = "fetch", s = "__oFetch_", p = "__oXMLHttpRequest_", c = "XMLHttpRequest";
    return a.ext(e.prototype, {
        removeHook: function(e, a) {
            return r && (a || this === r) ? (t[s] && (t[o] = t[s], delete t[s]), t[p] && (t[c] = t[p], 
            delete t[p]), r = null, this) : this;
        },
        addHook: function(e) {
            return !e && r ? this : (r || (function() {
                if ("function" == typeof t[o]) {
                    var e = t[o];
                    t[s] = e, t[o] = function(o, s) {
                        var p = 1 === arguments.length ? [ arguments[0] ] : Array.apply(null, arguments), c = r;
                        if (!c || !c.api) return e.apply(t, p);
                        if (s && ("HEAD" === s.method || "no-cors" === s.mode)) return e.apply(t, p);
                        var l = Date.now(), g = c._conf, d = (o && "string" != typeof o ? o.url : o) || "", u = d;
                        d = a.cutUrlSearch(d);
                        var f = (g.ignore || {}).ignoreApis;
                        if (!a.checkAPI(d, !0) || a.ignoreByRule(d, f)) return e.apply(t, p);
                        d = a.filterByRule(d, g.ignoreApiPath ? g.ignoreApiPath : g.apiHelper);
                        var h = g.enableLinkTrace, y = "", E = "", v = c.getConfig("pid"), I = null, D = c.getConfig("sample"), T = c.getConfig("linkType"), A = 1;
                        if (D && !c.sampling(D) && (A = 0), h) {
                            var S = "";
                            try {
                                S = location.origin ? location.origin : location.protocol + "//" + location.hostname + (location.port ? ":" + location.port : "");
                            } catch (R) {
                                S = "";
                            }
                            var b = a.checkSameOrigin(u, S);
                            if (c.getConfig("enableApiCors") || b) {
                                if (o && "string" != typeof o) try {
                                    if (p[0].headers && "function" == typeof p[0].headers.get && "function" == typeof p[0].headers.append) switch (T) {
                                      case "tracing":
                                        var k = p[0].headers.get("uber-trace-id");
                                        k ? y = k.split(":")[0] : (k = c.getUberTraceId(A), p[0].headers.append("uber-trace-id", k["uber-trace-id"]), 
                                        y = k.traceId), E = c.pageview;
                                        break;

                                      default:
                                        var m = p[0].headers.get("EagleEye-TraceID"), w = p[0].headers.get("EagleEye-SessionID"), H = p[0].headers.get("EagleEye-pAppName");
                                        m ? y = m : (y = c.getTraceId()["EagleEye-TraceID"], p[0].headers.append("EagleEye-TraceID", y)), 
                                        w ? E = w : (E = c.getPageviewId()["EagleEye-SessionID"], p[0].headers.append("EagleEye-SessionID", E)), 
                                        H || p[0].headers.append("EagleEye-pAppName", v);
                                    }
                                } catch (P) {
                                    a.warn("[retcode] fetch failed to set header, exception is :\n" + P);
                                }
                                if (s) switch (s.headers = s.headers ? s.headers : {}, T) {
                                  case "tracing":
                                    if (s.headers["uber-trace-id"]) y = s.headers["uber-trace-id"].split(":")[0]; else {
                                        var C = c.getUberTraceId(A);
                                        s.headers["uber-trace-id"] = C["uber-trace-id"], y = C.traceId;
                                    }
                                    E = c.getPageviewId()["EagleEye-SessionID"];
                                    break;

                                  default:
                                    s.headers["EagleEye-TraceID"] ? y = s.headers["EagleEye-TraceID"] : (y = c.getTraceId()["EagleEye-TraceID"], 
                                    s.headers["EagleEye-TraceID"] = y), s.headers["EagleEye-SessionID"] ? E = s.headers["EagleEye-SessionID"] : (E = c.getPageviewId()["EagleEye-SessionID"], 
                                    s.headers["EagleEye-SessionID"] = E), s.headers["EagleEye-pAppName"] || (s.headers["EagleEye-pAppName"] = v);
                                }
                            }
                        }
                        return e.apply(t, p).then(function(e) {
                            if (!c || !c.api) return e;
                            try {
                                if (!e || "function" != typeof e.clone) return e;
                                var t = e.clone(), r = t.headers;
                                if (r && "function" == typeof r.get) {
                                    var o = r.get("content-type");
                                    if (o && !/(text)|(json)/.test(o)) return e;
                                    if (!y) {
                                        var s = a.parseFetchHeaders(r);
                                        "object" == typeof s && s["eagleeye-traceid"] && (y = s["eagleeye-traceid"], I = "response");
                                    }
                                }
                                var f = Date.now() - l;
                                return t.text().then(function(e) {
                                    var o = a.getFetchSnapshot(p, e, r);
                                    t.ok ? i(c, g.parseResponse, d, u, e, t.status || 200, f, l, y, E, o, I, A) : c.api(d, !1, f, t.status || 404, t.statusText, l, y, E, o, n, I, A);
                                }), e;
                            } catch (R) {
                                return a.warn("[ARMS] fetch response error :", R), e;
                            }
                        })["catch"](function(e) {
                            if (!c || !c.api) throw e;
                            var t = Date.now() - l;
                            throw c.api(d, !1, t, e.name || "Error", e.message, l, y, E, {}, n), e;
                        });
                    }, t[o].toString = a.createFakeToString(o);
                }
            }(), function() {
                if ("function" == typeof t[c]) {
                    var e, o = t[c];
                    t[p] = o;
                    var s = function(t) {
                        var s = new o(t), p = r;
                        if (!p || !p.api || !s.addEventListener) return s;
                        var c, l, g, d = s.send, u = s.open, f = s.setRequestHeader, h = p._conf, y = (h.ignore || {}).ignoreApis, E = p.getConfig("enableLinkTrace"), v = "", I = "", D = "", T = null, A = p.getConfig("sample"), S = p.getConfig("linkType"), b = 1;
                        return A && !p.sampling(A) && (b = 0), s.open = function(t, r) {
                            e = t;
                            var n = 1 === arguments.length ? [ arguments[0] ] : Array.apply(null, arguments);
                            u.apply(s, n), g = r || "", l = a.cutUrlSearch(g);
                            var i = !a.checkAPI(l, !0) || a.ignoreByRule(l, y);
                            if (l = l ? a.filterByRule(l, h.ignoreApiPath ? h.ignoreApiPath : h.apiHelper) : "", 
                            !i && E) {
                                var o = "";
                                try {
                                    o = location.origin ? location.origin : location.protocol + "//" + location.hostname + (location.port ? ":" + location.port : "");
                                } catch (T) {
                                    o = "";
                                }
                                var c = a.checkSameOrigin(g, o);
                                if ((p.getConfig("enableApiCors") || c) && f && "function" == typeof f) switch (S) {
                                  case "tracing":
                                    var d = p.getUberTraceId(b);
                                    f.apply(s, [ "uber-trace-id", d["uber-trace-id"] ]), v = d.traceId, I = p.getPageviewId()["EagleEye-SessionID"];
                                    break;

                                  default:
                                    v = p.getTraceId()["EagleEye-TraceID"], f.apply(s, [ "EagleEye-TraceID", v ]), I = p.getPageviewId()["EagleEye-SessionID"], 
                                    f.apply(s, [ "EagleEye-SessionID", I ]), D = p.getConfig("pid"), f.apply(s, [ "EagleEye-pAppName", D ]);
                                }
                            }
                        }, s.send = function() {
                            c = Date.now();
                            var e = 1 === arguments.length ? [ arguments[0] ] : Array.apply(null, arguments);
                            d.apply(s, e);
                        }, a.on(s, "readystatechange", function() {
                            if (l && 4 === s.readyState) {
                                var t = Date.now() - c, r = a.getXhrSnapshot(g, e, s);
                                if (!v) {
                                    var o = a.parseXhrHeaders("function" == typeof s.getAllResponseHeaders && s.getAllResponseHeaders() || "");
                                    "object" == typeof o && o["eagleeye-traceid"] && (v = o["eagleeye-traceid"], T = "response");
                                }
                                if (s.status >= 200 && s.status <= 299) {
                                    var d = s.status || 200;
                                    if ("function" == typeof s.getResponseHeader) {
                                        var u = s.getResponseHeader("Content-Type");
                                        if (u && !/(text)|(json)/.test(u)) return;
                                    }
                                    s.responseType && "text" !== s.responseType ? p.api(l, !0, t, d, "", c, v, I, {}, n, T, b) : i(p, h.parseResponse, l, g, s.responseText, d, t, c, v, I, r, T, b);
                                } else p.api(l, !1, t, s.status || "FAILED", s.statusText, c, v, I, r, n, T, b);
                            }
                        }), s;
                    };
                    s.prototype = o.prototype, t[c] = s, t[c].toString = a.createFakeToString(c);
                }
            }()), r = this, this);
        },
        initHook: function() {
            return this.hasInitHook ? this : (this.getConfig("disableHook") || this.addHook(), 
            this.hasInitHook = !0, this);
        }
    }), e;
};